﻿using System;
using System.Threading.Tasks;
using DK.Abp.IdentityManagement.Blazor.BootstrapBlazorUI;
using DK.Abp.SettingManagement.Blazor.BootstrapBlazorUI;
using DK.Abp.TenantManagement.Blazor.BootstrapBlazorUI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MPS.Localization;
using MPS.MultiTenancy;
using Volo.Abp.UI.Navigation;
using Volo.Abp.Users;

namespace MPS.Blazor.Menus;

public class MPSMenuContributor : IMenuContributor
{
    private readonly IConfiguration _configuration;

    public MPSMenuContributor(IConfiguration configuration)
    {
        _configuration = configuration ?? throw new System.ArgumentNullException(nameof(configuration));
    }
    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        if (context.Menu.Name == StandardMenus.Main)
        {
            await ConfigureMainMenuAsync(context);
        }
        if (context.Menu.Name == StandardMenus.User)
        {
            await ConfigureUserMenuAsync(context);
        }
    }

    private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
    {
        var administration = context.Menu.GetAdministration();
        var l = context.GetLocalizer<MPSResource>();

        context.Menu.Items.Insert(
            0,
            new ApplicationMenuItem(
                MPSMenus.Home,
                l["Menu:Home"],
                "/",
                icon: "fas fa-home",
                order: 0
            )
        );

        if (MultiTenancyConsts.IsEnabled)
        {
            administration.SetSubItemOrder(TenantManagementMenuNames.GroupName, 1);
        }
        else
        {
            administration.TryRemoveMenuItem(TenantManagementMenuNames.GroupName);
        }

        var tables = new ApplicationMenuItem(
            "Tables",
            l["Menu:Tables"],
            icon: "fas fa-home"
        );
        tables.AddItem(new ApplicationMenuItem(
            "Tables.MainTable",
            l["Menu:MainTable"],
            url: "/maintable"
        ));
        administration.SetSubItemOrder(IdentityMenuNames.GroupName, 2);
        administration.SetSubItemOrder(SettingManagementMenus.GroupName, 3);
        context.Menu.AddItem(tables);

        return Task.CompletedTask;
    }

    private Task ConfigureUserMenuAsync(MenuConfigurationContext context)
    {
        var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
        var l = context.GetLocalizer<MPSResource>();
        var identityServerUrl = _configuration["AuthServer:Authority"] ?? "";

        if (currentUser.IsAuthenticated)
        {
            //TODO: Localize menu items
            context.Menu.AddItem(new ApplicationMenuItem("Account.Manage", l["AccountManage"], url: $"{identityServerUrl.EnsureEndsWith('/')}Account/Manage", order: int.MaxValue - 1001));
            context.Menu.AddItem(new ApplicationMenuItem("Account.Logout", l["Logout"], url: "/Account/Logout", order: int.MaxValue - 1000));
            //context.Menu.AddItem(new ApplicationMenuItem("Account/Manage", "我的账户", url: "/Account/Manage", order: int.MaxValue - 1001));
        }

        return Task.CompletedTask;
    }
}
